'''每次到来新的数据，提取各自需要的图片'''






import re
import os
import shutil
from turtle import pd
import cv2
import numpy as np

img_path_origin = r'/home/xys/larger_objet_Projects/20211120/'
img_save_path = r'/home/xys/larger_objet_Projects/data_xin/data_all/'

classes = ['0113','0157','0201','0202','0203','0204','0209','0210','0321','0352']

img_save = img_save_path
a = 0
p = '0'

for i in classes:
    keyword = i


    '''如果想分开文件夹装载各个标签的图片，将以下注释去掉即可'''
    #############################################
    # a = 0
    # p = '0'
    #
    # img_save_path = os.path.join(os.path.join(img_save, i))      #图片存储路径
    # if not os.path.isdir(img_save_path):
    #     os.mkdir(img_save_path)
    ##############################################

    for j in os.listdir(img_path_origin):
        fulldirct = os.path.join(img_path_origin, j)             #fulldirct：20211120子文件夹路径
        for root, dirs, files in os.walk(fulldirct): #fulldirct路径改变为图片路径跳出循环
            # root 表示当前正在访问的文件夹路径
            # dirs 表示该文件夹下的子目录名list
            # files 表示该文件夹下的文件list
            for file in files:   #遍历寻找keywords，每个文件夹只有一张，找到一张fulldirct路径就会改变，跳出循环找下一个文件夹
                if keyword==file:
                    p = str(a)
                    mm = os.path.join(img_save_path, p)
                    a = a+1
                    fulldirct = os.path.join(fulldirct, file)
                    shutil.copy(fulldirct, mm+".jpg")
